package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.id.generator.IdGenerator;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.enums.PurchaseOrderStatusEnum;
import com.ethink.order.api.info.PurchaseOrderDetailInfo;
import com.ethink.order.api.info.PurchaseOrderInfo;
import com.ethink.order.api.model.PurchaseDetailModel;
import com.ethink.order.api.model.PurchaseOrderModeModel;
import com.ethink.order.api.model.PurchaseOrderModel;
import com.ethink.order.api.model.PurchaseOrderWithPayModel;
import com.ethink.order.api.request.*;
import com.ethink.order.api.response.OverseasDeleveryPreQueryResponse;
import com.ethink.order.api.response.PurchaseOrderDetailQueryResponse;
import com.ethink.order.common.entity.PurchaseOrder;
import com.ethink.order.common.entity.PurchaseOrderDetail;
import com.ethink.order.common.mapper.PurchaseOrderDetailMapper;
import com.ethink.order.common.mapper.PurchaseOrderMapper;
import com.ethink.order.converter.PurchaseOrderConverter;
import com.ethink.order.converter.PurchaseOrderDetailConverter;
import com.ethink.order.manager.SaleChannelRequestManager;
import com.ethink.order.service.PurchaseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 采购单 Service接口实现
 *
 * @author default
 * @date 2022-10-31
 */
@Slf4j
@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {

    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private PurchaseOrderDetailMapper purchaseOrderDetailMapper;

    @Autowired
    private PurchaseOrderConverter purchaseOrderConverter;

    @Autowired
    private PurchaseOrderDetailConverter purchaseOrderDetailConverter;

    @Autowired
    private IdGenerator idGenerator;

    @Override
    public PurchaseOrder add(PurchaseOrderAddRequest request) {
        String orderNo = idGenerator.cg();
        PurchaseOrder purchaseOrder = BeanUtil.copyProperties(request, PurchaseOrder.class, "ids", "orderNo");
        purchaseOrder.setShouldPayAmount(purchaseOrder.getOrderPrice());
        purchaseOrder.setOrderNo(orderNo);
        purchaseOrderMapper.insert(purchaseOrder);
        return purchaseOrder;
    }

    @Override
    public Page<PurchaseOrderModel> purchaseOrderPageQuery(PurchaseOrderPageQueryRequest request) {
        SaleChannelRequestManager.fill(request, 0, 1, 2);
        return purchaseOrderMapper.purchaseOrderPageQuery(PageUtils.buildPage(request), request);
    }

    @Override
    public PurchaseOrderDetailQueryResponse queryPurchaseOrderDetail(Long id) {
        PurchaseOrderDetailQueryResponse response = new PurchaseOrderDetailQueryResponse();
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectById(id);
        if (ObjectUtil.isEmpty(purchaseOrder)) {
            return response;
        }
        PurchaseOrderWithPayModel purchaseOrderWithPayModel = BeanUtil.copyProperties(purchaseOrder, PurchaseOrderWithPayModel.class);
        response.setMain(purchaseOrderWithPayModel);
        List<PurchaseDetailModel> item = purchaseOrderDetailMapper.selectPurchaseDetailByOrderNo(purchaseOrder.getOrderNo());
        response.setItem(item);
        return response;
    }

    @Override
    public Integer purchaseOrderRefine(PurchaseOrderRefineRequest refineRequest) {
        PurchaseOrder purchaseOrder = BeanUtil.copyProperties(refineRequest, PurchaseOrder.class);
        purchaseOrder.setSuppleTime(new Date());
        return purchaseOrderMapper.updateById(purchaseOrder);
    }

    @Override
    public OverseasDeleveryPreQueryResponse overseasDeliveryPreQuery(Long id) {
        OverseasDeleveryPreQueryResponse response = new OverseasDeleveryPreQueryResponse();
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectById(id);
        PurchaseOrderInfo purchaseOrderInfo = purchaseOrderConverter.convertFrom(purchaseOrder);
        response.setPurchaseOrderInfo(purchaseOrderInfo);
        List<PurchaseOrderDetail> detailList = purchaseOrderDetailMapper.selectList(new LambdaQueryWrapper<PurchaseOrderDetail>()
                .eq(PurchaseOrderDetail::getOrderNo, purchaseOrder.getOrderNo()));
        List<PurchaseOrderDetailInfo> detailInfoList = new ArrayList<>();
        for (PurchaseOrderDetail purchaseOrderDetail : detailList) {
            PurchaseOrderDetailInfo purchaseOrderDetailInfo = purchaseOrderDetailConverter.convertFrom(purchaseOrderDetail);
            detailInfoList.add(purchaseOrderDetailInfo);
        }
        response.setPurchaseOrderDetailInfos(detailInfoList);
        return response;
    }

    @Override
    public void updateStatus(PurchaseOrderUpdateStatusRequest updateStatusRequest) {
        purchaseOrderMapper.update(null, new LambdaUpdateWrapper<PurchaseOrder>()
                .eq(PurchaseOrder::getId, updateStatusRequest.getId())
                .set(PurchaseOrder::getOrderStatus, updateStatusRequest.getOrderStatus()));
    }

    @Override
    public Integer purchaseWarehouseUpdate(PurchaseWarehouseUpdateRequest request) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectById(request.getId());
        return purchaseOrderMapper.update(null, new LambdaUpdateWrapper<PurchaseOrder>()
                .eq(PurchaseOrder::getId, request.getId())
                .set(PurchaseOrder::getSubWarehouseCode, request.getSubWarehouseCode())
                .set(PurchaseOrder::getSubWarehouseName, request.getSubWarehouseName()));
    }

    @Override
    public PurchaseOrderInfo getPurchaseOrderInfo(String orderNo) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectOne(
                new LambdaQueryWrapper<PurchaseOrder>().eq(PurchaseOrder::getOrderNo, orderNo));
        return purchaseOrderConverter.convertFrom(purchaseOrder);
    }

    @Override
    public Boolean purchaseOrderFinish(List<String> orderNo) {
        if (CollUtil.isEmpty(orderNo)) {
            return true;
        }
        return purchaseOrderMapper.update(null, new LambdaUpdateWrapper<PurchaseOrder>()
                .in(PurchaseOrder::getOrderNo, orderNo)
                .set(PurchaseOrder::getOrderStatus, PurchaseOrderStatusEnum.S4.getIntValue())
        ) > 0;
    }

    @Override
    public PurchaseOrderInfo queryMainById(Long id) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectById(id);
        return BeanUtil.copyProperties(purchaseOrder, PurchaseOrderInfo.class);
    }

    @Override
    public Boolean purchasePayTag(String orderNo) {
        return purchaseOrderMapper.update(null, new LambdaUpdateWrapper<PurchaseOrder>()
                .eq(PurchaseOrder::getOrderNo, orderNo)
                .set(PurchaseOrder::getHasPaid, true)
        ) > 0;
    }

    /**
     * 采购订单货品模式分页查询
     *
     * @param request
     * @return
     */
    @Override
    public Page<PurchaseOrderModeModel> purchaseOrderPageModeQuery(PurchaseOrderPageQueryModeRequest request) {
        SaleChannelRequestManager.fill(request, 0, 1, 2);
        return purchaseOrderMapper.purchaseOrderPageModeQuery(PageUtils.buildPage(request), request);
    }
}
